package com.dianrui.fmi.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

public class ForgetPasswordActivity extends BaseActivity {
  private ProgressDialog pd;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.login_mobile)
    EditText loginMobile;


    @BindView(R.id.save_btn)
    Button saveBtn;

    @Override
    public int getContentViewId() {
        return R.layout.activity_forgetpassword;
    }

    @Override
    public void initView() {
        pd=new ProgressDialog(this);
        title.setText(getString(R.string.forget_password));


        loginMobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                if (loginMobile.length()>0 && EmailFormat(loginMobile.getText().toString())) {
                    saveBtn.setBackgroundResource(R.drawable.login_orange_shape);
                    saveBtn.setEnabled(true);
                }else {
                    saveBtn.setBackgroundResource(R.drawable.gray_shape);
                    saveBtn.setEnabled(false);
                }
            }
        });

    }


    @OnClick({R.id.back, R.id.save_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.save_btn:
                if (StringUtils.isEmpty(loginMobile.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.please_input_your_email));
                }else if (!EmailFormat(loginMobile.getText().toString())) {
                    ToastUtil.showToast("Email incorrect");
                }else {
                    forgetPassword();
                }

                break;
                default:
                    break;
        }
    }

    private boolean EmailFormat(String email) {//邮箱判断正则表达式
        Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        Matcher mc = pattern.matcher(email);
        return mc.matches();
    }

    private void forgetPassword() {
        pd.setMessage(getString(R.string.submit));
        pd.show();
        pd.setCancelable(false);
        pd.setCanceledOnTouchOutside(false);
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("email",loginMobile.getText().toString());
        jsonObject.addProperty("type","3");
        OkHttpUtils.getInstance().postWithMemberToken(Url.EMAIL, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String response) {
                if (!StringUtils.isEmpty(response)) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                pd.dismiss();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                startActivity(new Intent(ForgetPasswordActivity.this,ForgetPasswordNextActivity.class)
                                        .putExtra("email",loginMobile.getText().toString()));
                            }else {
                                pd.dismiss();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    pd.dismiss();
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (pd != null && pd.isShowing()) {
            pd.dismiss();
        }
    }
}
